Imports System.Data
Imports System.Linq
Imports StartFrame.Web.BR.Helpers

Public MustInherit Class JsonAbm
    Inherits Global.StartFrame.BR.Abm

    Private _OrdenDefault As String

    Sub New(ByVal nombreOperador As String, _
      ByVal claveAcceso As String, _
      ByVal nombrePrograma As String, _
      ByVal tablaPrincipal As String,
     Optional OrdenDefault As String = "descripcion")
        MyBase.New(nombreOperador, claveAcceso, nombrePrograma, tablaPrincipal)
        _OrdenDefault = OrdenDefault
    End Sub

    Public Overridable Function List(Start As Integer, Limit As Integer, Orden As String, Filtro As String) As String
        Dim ds As DataSet
        Dim rs As New ResultSet
        Dim ar As New ArrayList
        Dim cOrden As OrdenColeccion = OrdenColeccion.Parse(Orden)
        Dim cFiltro As FiltroColeccion = FiltroColeccion.Parse(Filtro)

        ar.Add(Me.getTablaPrincipal())
        ar.Add(_OrdenDefault)
        ar.Add(Start)
        ar.Add(Limit)
        If cOrden.FirstOrDefault() Is Nothing Then
            ar.Add(DBNull.Value)
            ar.Add(DBNull.Value)
        Else
            ar.Add(cOrden.FirstOrDefault().Property)
            ar.Add(cOrden.FirstOrDefault().Direction)
        End If
        ar.Add(cFiltro.ToSQL)

        Try
            ds = DA.Sql.ExecSPDS(Common.Env.ConnectionString, "wp_List_Generic", ar)
            rs.LoadTable(ds.Tables(0))
            rs.total = ds.Tables(1).Rows(0)(0)
            rs.success = True
        Catch ex As Exception
            rs.message = ex.Message
            rs.success = False
        End Try

        Return Serializer.Serialize(rs)
    End Function

    Public Overridable Function ListDS(Start As Integer, Limit As Integer, Orden As String, Filtro As String) As DataSet
        Dim ds As DataSet
        Dim rs As New ResultSet
        Dim ar As New ArrayList
        Dim cOrden As OrdenColeccion = OrdenColeccion.Parse(Orden)
        Dim cFiltro As FiltroColeccion = FiltroColeccion.Parse(Filtro)

        ar.Add(Me.getTablaPrincipal())
        ar.Add(_OrdenDefault)
        ar.Add(Start)
        ar.Add(Limit)
        If cOrden.FirstOrDefault() Is Nothing Then
            ar.Add(DBNull.Value)
            ar.Add(DBNull.Value)
        Else
            ar.Add(cOrden.FirstOrDefault().Property)
            ar.Add(cOrden.FirstOrDefault().Direction)
        End If
        ar.Add(cFiltro.ToSQL)

        Try
            ds = DA.Sql.ExecSPDS(Common.Env.ConnectionString, "wp_List_Generic", ar)
        Catch ex As Exception
            Throw ex
        End Try
        Return ds
    End Function


    Public Function getNum(Talonario As String) As String
        Return New Global.StartFrame.BR.Utilitarios.Talonarios().getNum(Talonario, True)
    End Function

End Class
